pub mod invoke {
    use std::{thread, thread::sleep, time::Duration};

    use hotwatch::{Event, Hotwatch};
    use tauri::{Builder, Runtime, Window};


    #[tauri::command]
    fn greet(name: &str) {
        println!("Hello, {}!", name)
    }


    #[tauri::command]
    fn watch_dir<R: Runtime>(window: Window<R>, path: String) {
        thread::spawn(move || {
            let mut hotwatch = Hotwatch::new().expect("hotwatch failed to initialize!");
            hotwatch.watch(path, move |event: Event| {
                if let Event::Write(path) = event {
                    println!("other file has been changed!{:?}", path);
                    window.emit("dir_change", path.canonicalize().unwrap()).unwrap();
                }
            })
                .expect("failed to watch file!");
            loop {
                sleep(Duration::from_secs(2));
            }
        });
    }


    pub fn invoke_handler<R: Runtime>(d: Builder<R>) -> Builder<R> {
        d.invoke_handler(tauri::generate_handler![greet,watch_dir])
    }
}